package com.wctj.api.web.handle;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wctj.api.base.Result;
import com.wctj.api.base.enums.ApiCode;
import com.wctj.api.base.enums.BankEnums;
import com.wctj.api.base.enums.PayPlatformEnums;
import com.wctj.api.base.exception.CustomException;
import com.wctj.api.base.utils.OrderIdUtils;
import com.wctj.api.business.common.dto.*;
import com.zgzg.shop.pay.business.cmb.service.CmbPayManagerService;
import com.wctj.api.business.common.helper.PayResponseHelper;
import com.wctj.api.business.common.service.AbstractPayChannelService;
import com.wctj.api.business.common.service.AbstractPayManagerService;
import com.zgzg.shop.pay.business.huifu.service.HuiFuPayManagerService;
import com.wctj.api.infra.web.support.HttpRequestNoticeParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Service
public class PayManagerService extends AbstractPayManagerService {
    @Resource
    private PayResponseHelper payResponseHelper;
    @Resource
    private HuiFuPayManagerService huiFuPayManagerService;
    @Resource
    private CmbPayManagerService cmbPayManagerService;


    /**
     * 生成支付地址跳转信息
     *
     * @param param
     * @return
     */
    public Result toPay(PayOrderDTO param) {
        AbstractPayChannelService payChannelService = AbstractPayChannelService.PAY_CHANNEL_MAP.get(param.getPayChannel());
        if (Objects.isNull(payChannelService)) {
            throw new CustomException(ApiCode.PAY_PLATFORM_NOT_EXIST_FAIL);
        }
        // 支付渠道类型的参数校验
        payChannelService.validParam(param);
        // 支付交易Id
        String payOrderNum = OrderIdUtils.getOrderId();
        // 第三方支付平台，目前仅有汇付天下
        if (PayPlatformEnums.HUIFU_ADAPAY == param.getPayChannel().getType()) {
            return huiFuPayManagerService.toPay(param,payOrderNum);
        }
        return Result.fail(ApiCode.PAY_CHANNEL_NOT_EXIST_FAIL);
    }

    /**
     * 接收支付回调
     *
     * @param request
     * @param payMode
     * @return
     */
    public String payBack(HttpServletRequest request, String payMode) {

        return payResponseHelper.handle(new HttpRequestNoticeParams(request), payMode);
    }

    /**
     * 根据开发平台的支付订单号查询开发平台的支付信息
     * @param payOrderNo
     * @return
     */
    public DdwxPaymentRecordDTO infoByPayOrderNo(String payOrderNo) {

        return ddwxPaymentRecordService.findByPayOrderNumber(payOrderNo);
    }

    /**
     * 根据第三方平台的支付订单号查询第三方平台的支付对象信息
     * @param paymentId
     * @return
     */
    public Map<String,Object> infoByThirdPartyPaymentId(PaymentIdDTO paymentId) {

        return huiFuPayManagerService.infoByPaymentId(paymentId);
    }

    /**
     * 提现接口
     * 暂时支持 cmb 招行提现
     * @param withdrawalOrderDTO
     * @return
     */
    public Result<Object> toWithdrawal(WithdrawalOrderDTO withdrawalOrderDTO) {
        // 支付交易Id
        String payOrderNum = OrderIdUtils.getOrderId();
        if (PayPlatformEnums.CMB_PAY.getCode() == withdrawalOrderDTO.getCode()) {
            return cmbPayManagerService.toWithdrawal(withdrawalOrderDTO,payOrderNum);
        }
        return Result.fail(ApiCode.PAY_WITHDRAWAL_TYPE_NOT_EXIST_FAIL);
    }


    /**
     * 单笔支付转账
     * @param singleTransferDTO
     * @return
     */
    public Result<Object> toSingleTransfer(SingleTransferDTO singleTransferDTO) {
        // 支付交易Id
        String payOrderNum = OrderIdUtils.getOrderId();
        if (PayPlatformEnums.CMB_PAY.getCode() == singleTransferDTO.getCode()) {
            return cmbPayManagerService.toSingleTransfer(singleTransferDTO,payOrderNum);
        }
        return Result.fail(ApiCode.PAY_WITHDRAWAL_TYPE_NOT_EXIST_FAIL);
    }

    /**
     * 批量支付转账
     * @param batchTransferDTO
     * @return
     */
    public Result<Object> toBatchTransfer(BatchTransferDTO batchTransferDTO) {
        // 支付交易Id
        String payOrderNum = OrderIdUtils.getOrderId();
        if (PayPlatformEnums.CMB_PAY.getCode() == batchTransferDTO.getCode()) {
            return cmbPayManagerService.toBatchTransfer(batchTransferDTO,payOrderNum);
        }
        return Result.fail(ApiCode.PAY_WITHDRAWAL_TYPE_NOT_EXIST_FAIL);
    }

    /**
     * 查询支付订单记录列表
     * @param paymentRecordDTO
     * @return
     */
    public Result<Object> getPaymentRecordList(PaymentRecordDTO paymentRecordDTO) {
        Page<DdwxPaymentRecordDTO> paymentRecordList = ddwxPaymentRecordService.getPaymentRecordList(paymentRecordDTO);
        return Result.success(paymentRecordList);
    }

    /**
     * 获取 账号详细信息
     * @param accountDTO
     * @return
     */
    public Result<Object> getAccountDetail(AccountDTO accountDTO) {
        if (BankEnums.CMB.getCode().equals(accountDTO.getBankCode())) {
            return cmbPayManagerService.getAccountDetail(accountDTO);
        }
        return Result.fail(ApiCode.PAY_AFFILIATED_BANK_NOT_SUPPORT_SERVICES);
    }

    /**
     * 查询交易信息列表
     * @param transactionDTO
     * @return
     */
    public Result<Object> getTransactionList(TransactionDTO transactionDTO) {
        if (BankEnums.CMB.getCode().equals(transactionDTO.getBankCode())) {
            return cmbPayManagerService.getTransactionList(transactionDTO);
        }
        return Result.fail(ApiCode.PAY_AFFILIATED_BANK_NOT_SUPPORT_SERVICES);
    }

    /**
     * 电子回单异步查询
     * @param electronReceiptDTO
     * @return
     */
    public Result<Object> getElectronReceipt(ElectronReceiptDTO electronReceiptDTO) {
        if (BankEnums.CMB.getCode().equals(electronReceiptDTO.getBankCode())) {
            return cmbPayManagerService.getElectronReceipt(electronReceiptDTO);
        }
        return Result.fail(ApiCode.PAY_AFFILIATED_BANK_NOT_SUPPORT_SERVICES);
    }

    /**
     * 异步打印结果查询
     * @param electronReceiptResultDTO
     * @return
     */
    public Result<Object> getElectronReceiptResult(ElectronReceiptResultDTO electronReceiptResultDTO) {
        if (BankEnums.CMB.getCode().equals(electronReceiptResultDTO.getBankCode())) {
            return cmbPayManagerService.getElectronReceiptResult(electronReceiptResultDTO);
        }
        return Result.fail(ApiCode.PAY_AFFILIATED_BANK_NOT_SUPPORT_SERVICES);
    }

    /**
     * 代发批次查询
     * @param releasePayBatchDTO
     * @return
     */
    public Result<Object> getReleasePayBatch(ReleasePayBatchDTO releasePayBatchDTO) {
        if (BankEnums.CMB.getCode().equals(releasePayBatchDTO.getBankCode())) {
            return cmbPayManagerService.getReleasePayBatch(releasePayBatchDTO);
        }
        return Result.fail(ApiCode.PAY_AFFILIATED_BANK_NOT_SUPPORT_SERVICES);
    }


    /**
     * 代发明细查询
     * @param releasePayDetailDTO
     * @return
     */
    public Result<Object> getReleasePayDetail(ReleasePayDetailDTO releasePayDetailDTO) {
        if (BankEnums.CMB.getCode().equals(releasePayDetailDTO.getBankCode())) {
            return cmbPayManagerService.getReleasePayDetail(releasePayDetailDTO);
        }
        return Result.fail(ApiCode.PAY_AFFILIATED_BANK_NOT_SUPPORT_SERVICES);
    }


    /**
     * 代发明细对账单查询请求
     * @param releasePayReconciliationDTO
     * @return
     */
    public Result<Object> getReleasePayReconciliation(ReleasePayReconciliationDTO releasePayReconciliationDTO) {
        if (BankEnums.CMB.getCode().equals(releasePayReconciliationDTO.getBankCode())) {
            return cmbPayManagerService.getReleasePayReconciliation(releasePayReconciliationDTO);
        }
        return Result.fail(ApiCode.PAY_AFFILIATED_BANK_NOT_SUPPORT_SERVICES);
    }

    /**
     * 代发明细对账单处理结果查询请求
     * @param releasePayReconciliationResultDTO
     * @return
     */
    public Result<Object> getReleasePayReconciliationResult(ReleasePayReconciliationResultDTO releasePayReconciliationResultDTO) {
        if (BankEnums.CMB.getCode().equals(releasePayReconciliationResultDTO.getBankCode())) {
            return cmbPayManagerService.getReleasePayReconciliationResult(releasePayReconciliationResultDTO);
        }
        return Result.fail(ApiCode.PAY_AFFILIATED_BANK_NOT_SUPPORT_SERVICES);
    }

    public Result<Object> getEBankReconciliationPending(EBankReconciliationPendingDTO eBankReconciliationPendingDTO) {
        if (BankEnums.CMB.getCode().equals(eBankReconciliationPendingDTO.getBankCode())) {
            return cmbPayManagerService.getEBankReconciliationPending(eBankReconciliationPendingDTO);
        }
        return Result.fail(ApiCode.PAY_AFFILIATED_BANK_NOT_SUPPORT_SERVICES);
    }

    public Result<Object> getEBankReconciliationHandling(EBankReconciliationHandlingDTO eBankReconciliationHandlingDTO) {
        if (BankEnums.CMB.getCode().equals(eBankReconciliationHandlingDTO.getBankCode())) {
            return cmbPayManagerService.getEBankReconciliationHandling(eBankReconciliationHandlingDTO);
        }
        return Result.fail(ApiCode.PAY_AFFILIATED_BANK_NOT_SUPPORT_SERVICES);
    }

    public Result<Object> getEBankReconciliationBusiness(EBankReconciliationBusinessDTO eBankReconciliationBusinessDTO) {
        if (BankEnums.CMB.getCode().equals(eBankReconciliationBusinessDTO.getBankCode())) {
            return cmbPayManagerService.getEBankReconciliationBusiness(eBankReconciliationBusinessDTO);
        }
        return Result.fail(ApiCode.PAY_AFFILIATED_BANK_NOT_SUPPORT_SERVICES);
    }

    public Result<Object> getEBankReconciliationDetail(EBankReconciliationDetailDTO eBankReconciliationDetailDTO) {
        if (BankEnums.CMB.getCode().equals(eBankReconciliationDetailDTO.getBankCode())) {
            return cmbPayManagerService.getEBankReconciliationDetail(eBankReconciliationDetailDTO);
        }
        return Result.fail(ApiCode.PAY_AFFILIATED_BANK_NOT_SUPPORT_SERVICES);
    }


    /**
     * 企银批量支付批次查询
     * @param paymentBatchDTO
     * @return
     */
    public Result<Object> getPaymentBatch(PaymentBatchDTO paymentBatchDTO) {
        if (BankEnums.CMB.getCode().equals(paymentBatchDTO.getBankCode())) {
            return cmbPayManagerService.getPaymentBatch(paymentBatchDTO);
        }
        return Result.fail(ApiCode.PAY_AFFILIATED_BANK_NOT_SUPPORT_SERVICES);
    }


    /**
     * 企银批量支付明细查询
     * @param paymentDetailDTO
     * @return
     */
    public Result<Object> getPaymentDetail(PaymentDetailDTO paymentDetailDTO) {
        if (BankEnums.CMB.getCode().equals(paymentDetailDTO.getBankCode())) {
            return cmbPayManagerService.getPaymentDetail(paymentDetailDTO);
        }
        return Result.fail(ApiCode.PAY_AFFILIATED_BANK_NOT_SUPPORT_SERVICES);
    }

    /**
     * 重复调用
     * @param repeatInvokingDTO
     * @return
     */
    public Result<Object> repeatInvoking(RepeatInvokingDTO repeatInvokingDTO) {
        if (BankEnums.CMB.getCode().equals(repeatInvokingDTO.getBankCode())) {
            return cmbPayManagerService.repeatInvoking(repeatInvokingDTO);
        }
        return Result.fail(ApiCode.PAY_AFFILIATED_BANK_NOT_SUPPORT_SERVICES);
    }

}
